* Varable creations : period after training ------------------------------------

	* (1) Post training month indicators
	
		* (2.1): Individual months
		forvalues m = 1(1)12 {
			gen months_`m' = (month == `m')
		}
		
		* (2.2): Month blocks: Months 1-4, 5-8, and so on
		mat month_create = 1, 4 \ 5, 8 \ 9, 12 \ 1, 3 \ 4, 6 \ 7, 9 \ 10, 12 \ 1, 12		
		
		forvalues i = 1(1)`= rowsof(month_create)' {
		
			* Month identifiers
			local a = month_create[`i',1]
			local b = month_create[`i',2]
			gen months_`a'_`b' = inrange(month, `a', `b')
		
			* Interaction identifiers

			* Months X treatment interaction
			gen months_`a'_`b'_t 		= months_`a'_`b' * treatment
		
		}
		
	* (3) Days-off
	gen days_off_non_iod = days_off - days_iod
		
	
	* (4) Variables for TRR subsetting: Level 1 & 2 combined
	gen l12_trr_C = trr_nf_p2020_1or2_C
	gen l23_trr_C = trr_nf_p2020_2or3_C
		
		foreach var in ///
			trr_s_i trr_sa_injury trr_officer_i trr_s_h  trr_s_hos_i_ix {
				
			gen l12_`var'_C 		= lv1_`var'_C + lv2_`var'_C
		}
	
	
* (5) Create index variable ----------------------------------------------------

						
	forvalues period = 1(1)12 {
	di "`period'"
	
		preserve

			* Restrict the data set to the given period
			gen months_all = 1
			keep if months_`period' == 1
			
				* (1) PRS Outcomes
				foreach v in $prs_items {
				z_cal prs_`v'		
				}
				
					* PRS score without arrests
					egen prs_score = rowmean(z_prs*)
					
					* PRS score with arrest
					z_cal arrest_non_con_cop
					egen prs_score_w_arrs = rowmean(z_prs* z_arrest_non_con_cop)
				
				
				* (2) Complaints 
				foreach o in ac_w_exo_ef ac_w_exo_crv ///
							 ac_w_exo_vbl_ef_crv ac_w_exo_lock_clus ///
							 ac_w_exo_dv_clus ac_w_exo_opv_clus{
				z_cal `o'
				}
				
				* Several complaint index
				egen i_complaint_pri = rowmean(z_ac_w_exo_ef z_ac_w_exo_crv)
				egen i_complaint_sec = rowmean(z_ac_w_exo_ef z_ac_w_exo_crv total_w_exo_accusations)
				egen i_complaints	 = rowmean(z_ac_w_exo_vbl_ef_crv z_ac_w_exo_lock_clus z_ac_w_exo_dv_clus z_ac_w_exo_opv_clus)
				
				* (3) Tactics
				foreach t in ttt_avoid_C ttt_phys_C ttt_force_handcuff_C {
				z_cal `t'
				}
					
					* Reverse sign of TTT Avoid
					replace z_ttt_avoid_C = - z_ttt_avoid_C
					
					* Index
					egen ttt_score = rowmean(z_ttt_*)
				
				* (4) Injuries
				foreach v in l12_trr_s_i_C l12_trr_sa_injury_C l12_trr_s_h_C ///
							 l12_trr_s_i_S l12_trr_sa_injury_S l12_trr_s_h_S {
							 z_cal `v'
							 }
				
				egen injury_score 		= rowmean(z_l12_trr_s_i_C z_l12_trr_sa_injury_C)
				egen injury_h_score 	= rowmean(z_l12_trr_s_i_C z_l12_trr_sa_injury_C z_l12_trr_s_h_C)
				egen injury_h_score_S 	= rowmean(z_l12_trr_s_i_S z_l12_trr_sa_injury_S z_l12_trr_s_h_S)
				
				* (5) Days off
				z_cal days_iod
				z_cal days_off_non_iod
				egen days_iod_score = rowmean(z_days_iod z_l12_trr_sa_injury_C)
				egen days_all_score = rowmean(z_days_iod z_l12_trr_sa_injury_C z_days_off_non_iod)
				
				* (6) Rename variables
				rename ///
					(ttt_score prs_score prs_score_w_arrs ///
					injury_score injury_h_score injury_h_score_S ///
					days_iod_score days_all_score ///
					z_arrest_non_con_cop ///
					i_complaint_pri i_complaint_sec i_complaints) ///
				   (ttt_score_`period' prs_score_`period' prs_score_w_arrs_`period' ///
				   injury_score_`period' injury_h_score_`period' injury_h_score_S_`period' ///
				   days_iod_score_`period' days_all_score_`period' ///
				   z_arrest_non_con_cop_`period' i_complaint_pri_`period' ///
				   i_complaint_sec_`period' i_complaints_`period')
				   
				 foreach v in $prs_items{
				 rename z_prs_`v' z_prs_`v'_`period' 
				 }
		   
				* (7) Clean up
				keep employee_id year_month ///
					ttt_score_`period' prs_score_`period' prs_score_w_arrs_`period' ///
					injury_score_`period' injury_h_score_`period' injury_h_score_S_`period' ///
					days_iod_score_`period' days_all_score_`period' ///
					z_prs_*_`period' z_arrest_non_con_cop_`period' ///
					i_complaint_pri_`period' i_complaint_sec_`period' ///
					i_complaints_`period'
				
			tempfile score_`period'
			save `score_`period''

		restore
	}
	

* (6) Merging back index data --------------------------------------------------

	forvalues period = 1(1)12 {
	merge 1:1 employee_id year_month using `score_`period'', nogen
	}
	
* (7) Create index outcome -----------------------------------------------------
* For regression purpose

	* Index outcomes 	
	foreach outcome in ///
		prs_score_w_arrs ///
		ttt_score ///
		injury_score injury_h_score injury_h_score_S ///
		days_all_score days_iod_score ///
		z_prs_warrant z_prs_veh_rec z_prs_firearm z_prs_traf_stop z_prs_driv_stop z_prs_cont z_prs_anov ///
		z_prs_haz_cit z_prs_nonhaz_cit z_prs_curf_vio z_prs_cta z_prs_park_cit z_arrest_non_con_cop ///
		i_complaint_pri i_complaint_sec i_complaints {
			
			* Replace the values
			foreach cut in A {
			gen `outcome'_cut_`cut'm = .
			forvalues period = 1(1)12 {
				replace `outcome'_cut_`cut'm 	= `outcome'_`period' if months_`period' == 1
			}
			}
			
		}
		
* The loop line (159) was originally created to allow for other indexing scheme